package service

import (
	"bytes"
	"common/config"
	"common/utils"
	"encoding/json"
	"fmt"
	"framework/game"
	"github.com/smartwalle/alipay/v3"
	hall "hall/pay/entity"
	"io/ioutil"
	"log"
	"net/http"
	"time"
)

func PostData(url string, data any) (*hall.CommonReceive, error) {
	res := map[string]interface{}{
		"param": data,
	}
	reqData, err := json.Marshal(res)
	if err != nil {
		return nil, err
	}
	//resp, err := http.Post(fmt.Sprintf("http://192.168.1.6/api/%s", url), "application/json", bytes.NewBuffer(reqData))
	//payUrl := config.Conf.Pay.PayUrl
	payUrl := game.Conf.GameConfig["pay"]["value"].(map[string]any)["pay_url"].(string)
	resp, err := http.Post(fmt.Sprintf("%s%s", payUrl, url), "application/json", bytes.NewBuffer(reqData))
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	var CommonReceive hall.CommonReceive
	response, err := ioutil.ReadAll(resp.Body)
	errs := json.Unmarshal(response, &CommonReceive)
	if errs != nil {
		log.Println(response)
		return nil, err
	}
	return &CommonReceive, nil
}
func AlipayPostData(url string, data any) (*http.Response, error) {
	res := map[string]interface{}{
		"param": data,
	}
	reqData, err := json.Marshal(res)
	if err != nil {
		return nil, err
	}
	resp, err := http.Post(url, "application/json", bytes.NewBuffer(reqData))
	return resp, nil
}

func PayData(param *hall.Pay, exp ...int) hall.Pay {
	dev_info_json, _ := json.Marshal(hall.DevInfoJson{
		DevType: 2,
		IpAddr:  "",
		MAC:     "F0E1D2C3B4A5",
	})
	expTime := 600
	if len(exp) > 0 {
		expTime = exp[0]
	}
	currentTime := time.Now().Unix() // 当前时间
	//param.MerCustId = config.Conf.Wallet.MerCustID
	param.MerCustId = game.Conf.GameConfig["pay_wallet"]["value"].(map[string]any)["mer_cust_id"].(string)
	param.Version = 10
	param.OrderDate = utils.TimeOrDateAsYear(currentTime, "", "Ymd")
	param.OrderExpireTime = utils.TimeOrDateAsYear(currentTime+int64(expTime), "", "YmdHis") // 15分钟后退回
	//url := config.Conf.Pay.Url
	url := game.Conf.GameConfig["pay"]["value"].(map[string]any)["url"].(string)
	param.BgRetUrl = fmt.Sprintf("%sCallbackPay", url) // 回调地址
	param.DevInfoJson = string(dev_info_json)
	//param.Url = config.Conf.Wallet.Url + param.Url
	param.Url = game.Conf.GameConfig["pay_wallet"]["value"].(map[string]any)["url"].(string) + param.Url
	return *param
}

// JsonOrderResultData 订单支付同步返回信息
func JsonOrderResultData(data *hall.CommonReceive) hall.ReturnResSynchronous {
	dataSting := fmt.Sprintf("%s", data.Data)
	var HfpayOrderResult hall.ReturnResSynchronous
	json.Unmarshal([]byte(dataSting), &HfpayOrderResult)
	return HfpayOrderResult
}

// JsonHfResultData 处理结果返回信息
func JsonHfResultData(data *hall.CommonReceive) hall.ReturnResAsynchronous {
	dataSting := fmt.Sprintf("%s", data.Data)
	var HfpayResult hall.ReturnResAsynchronous
	json.Unmarshal([]byte(dataSting), &HfpayResult)
	return HfpayResult
}

func AliPay(method string) (*alipay.Client, error) {
	var appId string
	if method == "app" {
		appId = config.Conf.Alipay.App.AppId
	} else {
		appId = config.Conf.Alipay.Web.AppId
	}
	var privateKey = config.Conf.Alipay.PrivateKey
	// false 沙箱环境 true 正式环境
	var isProduction = true
	var client, err = alipay.New(appId, privateKey, isProduction)
	if err != nil {
		return nil, err
	}
	if method == "app" {
		// 加载应用公钥证书
		if err = client.LoadAppCertPublicKeyFromFile(config.Conf.Alipay.App.AppPublicCert); err != nil {
			return nil, err
		}
	} else {
		// 加载应用公钥证书
		if err = client.LoadAppCertPublicKeyFromFile(config.Conf.Alipay.Web.AppPublicCert); err != nil {
			return nil, err
		}
	}
	// 加载支付宝根证书
	if err = client.LoadAliPayRootCertFromFile(config.Conf.Alipay.AlipayRootCert); err != nil {
		return nil, err
	}
	// 加载支付宝公钥证书
	if err = client.LoadAlipayCertPublicKeyFromFile(config.Conf.Alipay.AlipayPublicCert); err != nil {
		return nil, err
	}
	return client, nil
}
